<template>
	<view class="container">
		<web-view :webview-styles="webviewStyles" @message="handleMessage" :src="webviewSrc">
		</web-view>
	</view>
</template>

<script>
	export default {
		name: "buuug7-img-cropper",
		data() {
			return {
				webviewStyles: {},
				platform: "",
				// #ifdef APP-PLUS
				webviewSrc: "/uni_modules/buuug7-img-cropper/hybrid/html/cropper/index.html",
				// #endif
				// #ifdef MP-WEIXIN
				webviewSrc: "https://zjgc-app.zijieshop.com/uploads/uni_models/cropper/index.html",
				// #endif
				// #ifdef H5
				webviewSrc: "https://zjgc-app.zijieshop.com/uploads/uni_models/cropper/index.html",
				// #endif
				
			};
		},
		onShareAppMessage(options) {
			// console.log(options.webViewUrl)
			console.log("onShareAppMessage",options)
		},
		mounted() {
			const {
				platform
			} = uni.getSystemInfoSync();
			this.platform = platform;
			console.log("platform", platform);

			if (platform === "windows" || platform === "mac") {
				this.handleH5Message();
			}
			/* if (platform === "devtools"){
				 this.handleMessage();
			} */
		},

		methods: {
			handleMessage(event) {
				console.log("调用handleMessage");
				const platform = this.platform;
				const data = event.detail.data[0];
				console.log("platform -- biubiubiu", platform);
				if (platform === "android" || platform === "ios") {
					const eventChannel = this.getOpenerEventChannel();
					eventChannel.emit("imgCropped", {
						data: data.dataUrl
					});
				} else {
					console.log("发射事件biubiubiu");
					uni.$emit("toWx", {
						data: data.dataUrl
					});
				}

			},

			handleH5Message(e) {
				console.log(`H5Message`);
				window.addEventListener("message", (event) => {
					const data = event.data.data;
					if (data && data.type === "croppedData") {
						const eventChannel = this.getOpenerEventChannel();
						eventChannel.emit("imgCropped", {
							data: data.dataUrl
						});
					}
				});
			},
		},
	};
</script>

<style></style>
